package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;

import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;

import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //向菜品表里面插入数据 同时也会向口味表里面插入菜品口味 A B C口味   两张表进行插入
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        System.out.println("dish is "+dish);
        dishMapper.insert(dish); //在sql中将id回显给对象
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //Page为PageHelper里面的对象
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO); //自动将查询得到的列表转为分页对象 返回的数据类型要对应
//        System.out.println("page is "+page);
//        System.out.println("pageResult is "+page.getResult());

        return new PageResult(page.getTotal(), page.getResult());
    }
//
    @Override
    @Transactional //事务
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);//后绪步骤实现
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //当前菜品处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否能够删除---是否被套餐关联了？？ 套餐_菜品表  他们是对多对的关系
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            //当前菜品被套餐关联了，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//        ids.forEach(id->{
//            dishMapper.deleteById(id); //删除菜品
//            dishFlavorMapper.deleteByDishId(id); //根据菜品id删除对应的口味  1个菜品 多个口味
//        });
        //优化 可以批量删除菜品
        // delete from dish_flavor where dish_id in (?,?,?)
        dishMapper.deleteByIds(ids);
        //同理 也可以根据批量删除 口味表
        dishFlavorMapper.deleteByDishIds(ids);



    }
//
    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id); //菜品id查找
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id); //在口味表中按照菜品id查找
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
//

    /**
     * 修改菜品以及对应的口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateWithFavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.update(dish); //更新菜品实体类
        // 删除口味，重新插入
        dishFlavorMapper.deleteByDishId(dishDTO.getId());//根据菜品id删除对应的口味

        List<DishFlavor> flavors = dishDTO.getFlavors(); //获取前端传俩的口味列表
        if(flavors!=null && flavors.size() > 0){
            for(DishFlavor flavor : flavors){ //插入口味数据
                 flavor.setDishId(dishDTO.getId()); //设置口味对于的dishId
            }
            dishFlavorMapper.insertBatch(flavors);

        }

    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //将对应id号的菜品状态进行修改为status
         Dish dish = new Dish();
         dish.setId(id);
         dish.setStatus(status);
         dishMapper.update(dish);
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        //根据菜品分类id查询菜品
        List<Dish> dishList = dishMapper.getByCategoryId(categoryId);

        return dishList;
    }


//    @Override
//    public List<Dish> getByCategoryId(Long categoryId) {
////        List<Dish> dishList = dishMapper.getByCategoryId(categoryId);
////        return dishList;
//        Dish dish = Dish.builder()
//                .categoryId(categoryId)
//                .status(StatusConstant.ENABLE)
//                .build();
//        return dishMapper.list(dish);
//    }


    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        dishList.forEach(d -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        });
        return dishVOList;
    }
}
